package cqcvc.edu.cn.dao;

import cqcvc.edu.cn.base.BaseDao;
import cqcvc.edu.cn.entity.ActivityInfo;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ActivityDao {
    public List<ActivityInfo> findAll(){
        try{
            Statement statement= BaseDao.getConn().createStatement();
            String sql="select * from activity_info";
            ResultSet rs=statement.executeQuery(sql);
            return getList(rs);
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }

    public ActivityInfo findById(Long id)
    {
        try{
            Statement statement= BaseDao.getConn().createStatement();
            String sql="select * from activity_info where id="+id;
            ResultSet rs=statement.executeQuery(sql);
            List<ActivityInfo> list=getList(rs);
            if(list.size()>0)
            {
                return list.get(0);
            }
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
        return null;
    }

    public List<ActivityInfo> getList(ResultSet rs)
    {
        List<ActivityInfo> list=new ArrayList<>();
        if(rs==null)
        {
            return list;
        }
        try{
             while (rs.next())
             {
                 ActivityInfo item=new ActivityInfo();
                 item.setId(rs.getLong("id"));
                 item.setActTitle(rs.getString(2));
                 item.setActExtra(rs.getString(3));
                 item.setActDetail(rs.getString(4));
                 item.setActPic(rs.getBytes(5));
                 item.setActPrice(rs.getFloat(6));
                 item.setStartDate(rs.getDate(7));
                 item.setEndDate(rs.getDate(8));
                 item.setCreateDate(rs.getDate(9));
                 item.setShowOrder(rs.getInt(10));
                 item.setIfShow(rs.getString(11));
                 list.add(item);
             }
             return list;
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
}
